package homework2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

//自定义的工具类
public class JDBCUtil {
	private JDBCUtil() {
		
	}
	//获得properties中的数据
    private static ResourceBundle rb=ResourceBundle.getBundle("demo1.jdbc");
    
    private static String driver=rb.getString("driver");
    private static String url=rb.getString("url");
    private static String user=rb.getString("user");
    private static String psd=rb.getString("psd");
    
    static {
    	try {
			Class.forName(driver);//只执行一次
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	
	public static Connection getConnection() {
		try {
			return DriverManager.getConnection(url, user, psd);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public static void close(Connection conn,Statement st) {
		close(conn,st,null);
	}
	public static void close(Connection conn,Statement st,ResultSet rs) {
			
			try {
				if(rs!=null) {
				rs.close();
				rs=null;
				}
				if(st!=null) {
					st.close();
					st=null;
				}
				if(conn!=null) {
					conn.close();
					conn=null;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}	
	public static void main(String[] args) {
		Connection conn=JDBCUtil.getConnection();
		System.out.println(conn);
	}
}
